package manager;

import java.util.HashMap;
import java.util.Iterator;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;

public class ManagerPlayers {
	/**
	 * 单例
	 */
	private static ManagerPlayers					instance	= new ManagerPlayers();
	
	private HashMap<Integer, Player>	playerList	= new HashMap<Integer, Player>();
	
	static public ManagerPlayers getInstance() {
		return instance;
	}
	
	/**
	 * 玩家加入游戏
	 * 
	 * @param ctx
	 * @param player
	 * @return Player
	 */
	public void add(Integer ctx, Player player) {
		playerList.put(ctx, player);
	}
	
	/**
	 * 玩家离开游戏
	 * 
	 * @param ctx
	 * @return Player
	 */
	public Player remove(Integer ctx) {
		Player player = playerList.remove(ctx);
		// removePlayer(player);
		return player;
	}
	
 
	
	/**
	 * 根据玩家ID获取Player对象
	 * 
	 * @param uid
	 * @return Player
	 */
	public Player getPlayerByid(int id) {
		if (id <= 0) {
			return null;
		}
		
		for (Iterator<Player> iterator = playerList.values().iterator(); iterator.hasNext();) {
			Player player = (Player) iterator.next();
			if (player.id == id) {
				return player;
			}
		}
		
		return null;
	}
	
	public Player getPlayerByChan(Channel channel){
		if(channel==null){
			return null;
		}
		
		for (Iterator<Player> iterator = playerList.values().iterator(); iterator.hasNext();) {
			Player player = (Player) iterator.next();
			if (player.ctx == channel) {
				return player;
			}
		}
		
		return null;
	}
	
}
